import auth from "$lib/server/auth.js"; import Recipe from "$lib/server/models/Recipe.js"; import {error, json} from "@sveltejs/kit"; export async function POST({request, cookies, params}){ const user = await auth(cookies, false); const body = await request.json(); if(!body.name) error(400, "Recipe name required"); if(body.ingredients.length <= 0) error(400, "Ingredients required"); if(body.steps.length <= 0) error(400, "Preparation steps required"); for(let i = 0; i < body.ingredients.length; i++){ if(body.ingredients[i].quantity <= 0) error(400, "Quantity must be greater than 0"); if(!body.ingredients[i].name) error(400, "Ingredient name required"); } const recipe = await Recipe.findOne({_id: params.recipeId}); recipe.name = body.name; recipe.ingredients = body.ingredients; recipe.steps = body.steps; recipe.notes = body.notes; recipe.private = body.private; recipe.updatedAt = new Date(); await recipe.save(); return json({msg: "success"}, {status: 200}); } export async function DELETE({cookies, params}){ const user = await auth(cookies, false); let recipe; try{ recipe = await Recipe.findOne({_id: params.recipeId}); if(!recipe || user._id.toString() !== recipe.user.toString()) error(403, "Forbidden"); await Recipe.deleteOne({_id: params.recipeId}); }catch(e){ if(e.status === 403) error(403, "Forbidden"); error(500, "Internal Server Error"); } return json({msg: "success"}, {status: 200}); }